<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xi="http://www.w3.org/2001/XInclude" version="1.0">


  <xsl:template match="/">
    <xsl:apply-templates select="*"/>
  </xsl:template> 

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template> 

  <xsl:template match="*[contains(@class, ' map/topicref ')]">
    <xsl:element name="{name()}">
      <xsl:apply-templates select="@*|node()"/>
      <xsl:if test="@href != ''">
        <included>
          <xi:include href="../documents/{@href}"/>
        </included>
      </xsl:if>
    </xsl:element>
  </xsl:template>

  <xsl:template match="*[@conref]">
    <!-- 
       See the specs for an explanation on #topicid/elementid:
       http://docs.oasis-open.org/dita/v1.1/OS/langspec/common/theconrefattribute.html 

       FIXME "document-uri" is an uri, not just a relative path. Use a protocol
       instead and have setLoadIncludedXMLFunction handle the protocol 
    -->
    <xsl:variable name="document-uri">
      <xsl:choose>
        <xsl:when test="substring-before(@conref, '#') != ''">
          <xsl:text>../documents/</xsl:text><!-- FIXME use a protocol here -->
          <xsl:value-of select="substring-before(@conref, '#')"/>
        </xsl:when>
        <xsl:otherwise></xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    
    <xsl:variable name="hashref">
      <xsl:value-of select="substring-after(@conref, '#')"/>
    </xsl:variable>
    <xsl:variable name="hashref-elementid">
      <xsl:value-of select="substring-after($hashref, '/')"/>
    </xsl:variable>
    <xsl:variable name="hashref-topicid">
      <xsl:value-of select="substring-before($hashref, '/')"/>
    </xsl:variable>

    <xsl:variable name="xpath">
      <xsl:choose>
        <xsl:when test="$hashref-elementid != ''">
          <xsl:text>//*[@id = '</xsl:text>
          <xsl:value-of select="$hashref-topicid"/>
          <xsl:text>']//*[@id = '</xsl:text>
          <xsl:value-of select="$hashref-elementid"/>
          <xsl:text>']</xsl:text>
        </xsl:when>
        <xsl:when test="$hashref != ''">
          <xsl:text>//*[@id='</xsl:text>
          <xsl:value-of select="$hashref"/>
          <xsl:text>']</xsl:text>
        </xsl:when>
        <!-- No #hash, but with a uri: -->
        <!-- FIXME Well, we should be looking first the first topic in the document: -->
        <xsl:when test="$document-uri != ''">/*[1]</xsl:when>
        <!-- No #hash, no uri, ie an empty conref: -->
        <xsl:otherwise></xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:element name="{name()}">
      <xsl:apply-templates select="@*"/>
      <included-conref-content>
        
        <xsl:choose>
          <xsl:when test="$document-uri != '' and $xpath != ''">
            <xi:include href="{ $document-uri }" xpointer="xpointer({ $xpath })"></xi:include>
          </xsl:when>
          <!-- Local references, no need to xinclude: -->
          <xsl:when test="$hashref-elementid != ''">
            <xsl:copy-of select="//*[$hashref-topicid]//*[@id = $hashref-elementid]"/>
          </xsl:when>
          <xsl:when test="$hashref-topicid != ''">
            <xsl:copy-of select="//*[$hashref-topicid]"/>
          </xsl:when>
          <!-- Xopus prevents loading itself here: <xsl:when test="$xpath != ''">
            <xi:include href="{ /@xml:base }" xpointer="xpointer({ $xpath })"></xi:include>
          </xsl:when> -->
          <xsl:otherwise></xsl:otherwise>
        </xsl:choose>

        <original-content>
          <xsl:apply-templates select="node()"/>
        </original-content>

      </included-conref-content>
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>
